package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.SetmealDao;
import com.atguigu.entity.PageResult;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);

        Page<Setmeal> page = setmealDao.findPage(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<TravelGroup> findAllGroup() {

        List<TravelGroup> list = setmealDao.findAllGroup();

        return list;
    }

    @Override
    public void add(Setmeal setmeal, Integer[] travelgroupIds) {
        setmealDao.add(setmeal);

        setGroupSetmeal(travelgroupIds,setmeal.getId());

    }

    public void setGroupSetmeal(Integer[] travelgroupIds , Integer setmealId){
        if(travelgroupIds.length > 0){
            Map<String, Integer> map = new HashMap<>();
            for (Integer travelgroupId : travelgroupIds) {
                map.put("setmealId",setmealId);
                map.put("travelgroupId",travelgroupId);
                setmealDao.setGroupSetmeal(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        setmealDao.delete(id);
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setmealDao.getSetmeal();
    }

    @Override
    public int getGroupSetmealById(Integer id) {
        return setmealDao.getGroupSetmealById(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public Setmeal findByIdEx(Integer id) {
        return setmealDao.findById(id);
    }
}
